package com.smartrobot.smartrobot.pagerHolder;



import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.SmartRobotApplication;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.AboutUsAcitivity;
import com.smartrobot.smartrobot.activity.CretaOrganActivity;
import com.smartrobot.smartrobot.activity.PersonalMessageActivity;
import com.smartrobot.smartrobot.activity.SettingAcitivity;
import com.smartrobot.smartrobot.activity.ShareSoftAcitivity;
import com.smartrobot.smartrobot.activity.ShowBigQrActivity;
import com.smartrobot.smartrobot.util.HttpUtil;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.QRCode;
import com.smartrobot.smartrobot.util.User;
import org.json.JSONObject;

public class PersonalPagerHolder extends Fragment implements BaseFragment,View.OnClickListener{

	private View view;
	private boolean isFirst = true;
    private boolean isCreate = false;
    private CircularImageView head;
    private TextView createOrgan,progressTxt,nickName;
    private LinearLayout centerLayout;
    private RelativeLayout aboutUs,personMessage,shareSoft,setting;
    private String headPath;
    static public Bitmap qrBitmap;
    private Context context;
	
	private ImageView qrCodeImg;
	QRCode qrCode ;
	
	public PersonalPagerHolder() {
	}

    /**
     * 返回的view为滑动显示的view
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.page_main_person_center,container,false);

        isFirst = true;
        isCreate = true;
        ImageLoaderApi.initImageLoaderApi(getActivity());
        init();

        Log.d("==========>","fragment 显示  "+ "个人中心");

        return view;
    }

    @Override
    public void onStart() {

//        init();

        if(nickName != null){

            if(!User.getInstance().getNickname().equals("未填写"))
              nickName.setText(User.getInstance().getNickname());
            else
              nickName.setText(User.getInstance().getPhone());


        }

        if(head != null && !User.getInstance().getHead().equals(headPath)){//非空，而且头像改变过才改变

            if(!User.getInstance().getHead().contains("/storage"))
                ImageLoader.getInstance().displayImage(Common.baseUrl + User.getInstance().getHead(), head, ImageLoaderApi.getOptions());
            else
            {
                Bitmap bitmap = BitmapFactory.decodeFile(User.getInstance().getHead());
                head.setImageBitmap(bitmap);

            }

        }

        super.onStart();
    }


    public void init(){

        if(isCreate)
		if(isFirst){

//            if(getActivity() == null) throw new NullPointerException();

			isFirst = false;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

			qrCodeImg = (ImageView)view.findViewById(R.id.center_qrCode);
            progressTxt = (TextView)view.findViewById(R.id.person_progressTxt);
            centerLayout = (LinearLayout)view.findViewById(R.id.center_layout);
            nickName = (TextView)view.findViewById(R.id.center_nickName);
            aboutUs = (RelativeLayout)view.findViewById(R.id.center_about_us);
            shareSoft = (RelativeLayout)view.findViewById(R.id.center_share_soft);
            setting = (RelativeLayout)view.findViewById(R.id.center_settting);
            personMessage = (RelativeLayout)view.findViewById(R.id.center_personal_message);
            head = (CircularImageView)view.findViewById(R.id.center_head);

                    headPath = User.getInstance().getHead();
//            createOrgan = (TextView)view.findViewById(R.id.create_organ);

             if(!User.getInstance().getHead().contains("/storage"))
                        ImageLoader.getInstance().displayImage(Common.baseUrl + User.getInstance().getHead(), head, ImageLoaderApi.getOptions());
                    else
                    {
                        Bitmap bitmap = BitmapFactory.decodeFile(User.getInstance().getHead());
                        head.setImageBitmap(bitmap);

                    }

			qrCode = new QRCode();
            if(User.getInstance() != null  && !User.getInstance().getId().equals("未登录")){//先看一下是否登录了

				JSONObject user = new JSONObject();
				try{
					user.put("ID",User.getInstance().getId());
					user.put("USER_ACCOUNT",User.getInstance().getPhone());
					user.put("NICK_NAME",User.getInstance().getNickname());

				}catch (Exception e){
					e.printStackTrace();
					Log.e("===========用户信息转json时出错========",e.getMessage());
				}

                qrBitmap = qrCode.createQRImage(user.toString());
                qrCodeImg.setImageBitmap(qrBitmap);
                if(!User.getInstance().getNickname().equals("未填写"))
                  nickName.setText(User.getInstance().getNickname());
                else
                    nickName.setText(User.getInstance().getPhone());
                qrCodeImg.setVisibility(View.VISIBLE);

            }else if(User.getUserInfo(getActivity())  && !User.getInstance().getId().equals("未登录")){//没有登录就看一下配置文件
                System.out.println("查找数据成功");
                qrBitmap = qrCode.createQRImage(User.getInstance().toString());
                qrCodeImg.setImageBitmap(qrBitmap);
                if(!User.getInstance().getNickname().equals("未填写"))
                    nickName.setText(User.getInstance().getNickname());
                else
                    nickName.setText(User.getInstance().getPhone());
                qrCodeImg.setVisibility(View.VISIBLE);
            }else//都没有就不显示二维码
            {
                System.out.println("无法读入数据");
                nickName.setText("未登录");
                qrCodeImg.setVisibility(View.GONE);
            }



            progressTxt.setVisibility(View.GONE);
            centerLayout.setVisibility(View.VISIBLE);
            qrCodeImg.setOnClickListener(PersonalPagerHolder.this);
            aboutUs.setOnClickListener(PersonalPagerHolder.this);
            personMessage.setOnClickListener(PersonalPagerHolder.this);
            setting.setOnClickListener(PersonalPagerHolder.this);
            shareSoft.setOnClickListener(PersonalPagerHolder.this);

//            createOrgan.setOnClickListener(PersonalPagerHolder.this);

                }
            },0);

		}
		
	}

    public void setContext(Context context){
        this.context = context;
    }

    @Override
    public void onClick(View view) {

        switch(view.getId()){
//            case R.id.create_organ:
//
//                Intent intent = new Intent();
//                intent.setClass(context, CretaOrganActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(intent);
//
//                break;

            case R.id.center_qrCode:

                Intent intent = new Intent();
                intent.setClass(getActivity(), ShowBigQrActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);

                break;

            case R.id.center_about_us:

                Intent aboutIntent = new Intent();
                aboutIntent.setClass(getActivity(), AboutUsAcitivity.class);
                aboutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(aboutIntent);

                break;

            case R.id.center_share_soft:

                Intent shareIntent = new Intent();
                shareIntent.setClass(getActivity(), ShareSoftAcitivity.class);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(shareIntent);

                break;

            case R.id.center_settting:

                Intent settingIntent = new Intent();
                settingIntent.setClass(getActivity(), SettingAcitivity.class);
                settingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(settingIntent);

                break;

            case R.id.center_personal_message:

                Intent centerIntent = new Intent();
                centerIntent.setClass(getActivity(), PersonalMessageActivity.class);
                centerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(centerIntent);

                break;


        }

    }
}
